﻿/*----------------------------------------------------------------
// Copyright (C) 2011 苏富特健康科技
// 版权所有。 
//
// 文件名：CookieHandler.cs
// 文件功能描述：cookie操作帮助
//
// 
// 创建标识：2012-06-21 张强
//
// 变更标识：｛日期｝ ｛变更人｝
// 变更描述：｛变更详情｝
//
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace WebHelper.SessionHandler
{
    /// <summary> 
    /// Cookie操作类 
    /// </summary> 
    public class CookieHandler : ISessionHandler
    {

        #region 保存Cookie
        /// <summary>
        /// 保存会话信息
        /// </summary>
        /// <param name="key">识别名</param>
        /// <param name="value">值</param>
        /// <param name="timeOut">超时时间，分钟</param>
        public void CreateSession<T>(string key, T value, int timeOut)
        {
            HttpCookie myCookie = new HttpCookie(key);
            DateTime now = DateTime.Now;
            myCookie.Value = ConvertObjectToString<T>(value);
            if (timeOut != 0)
            {
                myCookie.Expires = now.AddMinutes(timeOut);
            }
            if (HttpContext.Current.Response.Cookies[key] != null)
            {
                HttpContext.Current.Response.Cookies.Remove(key);
            }
            HttpContext.Current.Response.Cookies.Add(myCookie);
        }



        /// <summary>
        /// 序列化Cookie值为字符串
        /// </summary>
        /// <param name="CookieValue">Cookie值</param>
        /// <returns>Cookie值为字符串</returns>
        private static string ConvertObjectToString<T>(T CookieValue)
        {
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            bf.Serialize(ms, CookieValue);
            byte[] result = new byte[ms.Length];
            result = ms.ToArray();
            string temp = System.Convert.ToBase64String(result);
            ms.Flush();
            ms.Close();
            return temp;

        }

        #endregion


        #region 取得Cookie


        /// <summary>
        /// 获取会话信息
        /// </summary>
        /// <param name="key">识别名</param>
        /// <returns></returns>
        public T GetSession<T>(string key)
        {
            HttpCookie myCookie = HttpContext.Current.Request.Cookies[key];
            if (myCookie != null)
            {
                return ConvertStringToObject<T>(myCookie.Value);
            }
            else
            {
                return default(T);
            }
        }

        /// <summary>
        /// Cookie值为字符串反序列化为对象
        /// </summary>
        /// <param name="value">Cookie值为字符串</param>
        /// <returns>Cookie值对象</returns>
        private static T ConvertStringToObject<T>(string value)
        {

            byte[] b = System.Convert.FromBase64String(value);

            MemoryStream ms = new MemoryStream(b, 0, b.Length);

            BinaryFormatter bf = new BinaryFormatter();


            return (T)bf.Deserialize(ms);

        }

        #endregion


        #region 清除Cookie

        /// <summary>
        /// 清除会话信息
        /// </summary>
        /// <param name="key">识别名</param>
        public void ClearSession(string key)
        {
            HttpCookie myCookie = new HttpCookie(key);
            DateTime now = new DateTime(1900,1,1);
            myCookie.Expires = now;
            HttpContext.Current.Response.Cookies.Add(myCookie);
        }

        #endregion



    }
}
